// ================== TextEncoder 实现 ==================
export class TextEncoder {
    encode(str) {
        const bytes = [];
        for (let i = 0; i < str.length; i++) {
            let code = str.charCodeAt(i);

            // 处理代理对（四字节字符）
            if (code >= 0xD800 && code <= 0xDBFF) {
                const high = code;
                const low = str.charCodeAt(++i);
                code = (high - 0xD800) * 0x400 + (low - 0xDC00) + 0x10000;
            }

            // 编码为 UTF-8 字节
            if (code < 0x80) {
                bytes.push(code);
            } else if (code < 0x800) {
                bytes.push(0xC0 | (code >> 6));
                bytes.push(0x80 | (code & 0x3F));
            } else if (code < 0x10000) {
                bytes.push(0xE0 | (code >> 12));
                bytes.push(0x80 | ((code >> 6) & 0x3F));
                bytes.push(0x80 | (code & 0x3F));
            } else {
                bytes.push(0xF0 | (code >> 18));
                bytes.push(0x80 | ((code >> 12) & 0x3F));
                bytes.push(0x80 | ((code >> 6) & 0x3F));
                bytes.push(0x80 | (code & 0x3F));
            }
        }
        return new Uint8Array(bytes);
    }
}

// ================== TextDecoder 实现 ==================
export class TextDecoder {
    constructor(encoding = 'utf-8') {
        if (encoding.toLowerCase() !== 'utf-8') {
            throw new Error('仅支持 UTF-8 编码');
        }
    }

    decode(uint8Array) {
        let str = '';
        let i = 0;

        while (i < uint8Array.length) {
            const byte1 = uint8Array[i++];

            // 单字节字符 (0xxxxxxx)
            if (byte1 < 0x80) {
                str += String.fromCharCode(byte1);
                continue;
            }

            // 双字节字符 (110xxxxx 10xxxxxx)
            if ((byte1 & 0xE0) === 0xC0) {
                const byte2 = uint8Array[i++];
                const code = ((byte1 & 0x1F) << 6) | (byte2 & 0x3F);
                str += String.fromCharCode(code);
                continue;
            }

            // 三字节字符 (1110xxxx 10xxxxxx 10xxxxxx)
            if ((byte1 & 0xF0) === 0xE0) {
                const byte2 = uint8Array[i++];
                const byte3 = uint8Array[i++];
                const code = ((byte1 & 0x0F) << 12) |
                    ((byte2 & 0x3F) << 6) |
                    (byte3 & 0x3F);
                str += String.fromCharCode(code);
                continue;
            }

            // 四字节字符 (11110xxx 10xxxxxx 10xxxxxx 10xxxxxx)
            if ((byte1 & 0xF8) === 0xF0) {
                const byte2 = uint8Array[i++];
                const byte3 = uint8Array[i++];
                const byte4 = uint8Array[i++];
                const code = ((byte1 & 0x07) << 18) |
                    ((byte2 & 0x3F) << 12) |
                    ((byte3 & 0x3F) << 6) |
                    (byte4 & 0x3F);

                // 转换为代理对
                const high = Math.floor((code - 0x10000) / 0x400) + 0xD800;
                const low = ((code - 0x10000) % 0x400) + 0xDC00;
                str += String.fromCharCode(high, low);
                continue;
            }

            // 无效字节处理（默认替换为 U+FFFD）
            str += '\uFFFD';
        }

        return str;
    }
}
